package com.ynkbny.web.controller;

import com.ynkbny.base.PageResult;
import com.ynkbny.domain.DevicePoint;
import com.ynkbny.domain.pojo.DevicePointPaginReqVO;
import com.ynkbny.domain.pojo.DevicePointPaginResponseVO;
import com.ynkbny.domain.pojo.DevicePointVO;
import com.ynkbny.service.DevicePointService;
import com.ynkbny.util.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备点位表-控制层
 *
 * @author : juzipi123
 * @date : 2023-8-4
 */
@Slf4j
@Api(tags = "设备点位表对象功能接口")
@RestController
@RequestMapping("/devicePoint")
public class DevicePointController {

    @Resource
    private DevicePointService devicePointService;

    /**
     * 通过 条件查询 数据
     *
     * @author : juzipi123
     * @param vo 条件
     * @return 实例对象
     */
    @ApiOperation("通过 条件查询 数据")
    @GetMapping("/queryByDevicePointPaginVO")
    public Res<?> queryByDevicePointPaginVO(DevicePointVO vo) {
        log.info("请求参数：{}",vo);
        List<DevicePoint> devicePoints = devicePointService.queryByDevicePointPaginVO(vo);
        log.info("返回值：{}",devicePoints);
        return Res.ok(devicePoints);
    }

    /**
     * 新增数据
     *
     * @author : juzipi123
     * @param devicePoint 实例对象
     * @return 是否成功
     */
    @ApiOperation("新增数据")
    @PostMapping("/save")
    public Res<String> save(@RequestBody DevicePoint devicePoint) {
        log.info("请求参数：{}",devicePoint);
        boolean save = devicePointService.saveOrUpdate(devicePoint);
        log.info("返回值：{}",save);
        return  save ? Res.ok() : Res.fail();
    }

    /**
     * 更新数据
     *
     * @author : juzipi123
     * @param devicePoint 实例对象
     * @return 是否成功
     */
    @ApiOperation("更新数据")
    @PutMapping("/updateById")
    public Res<String> updateById(@RequestBody DevicePoint devicePoint) {
        log.info("请求参数：{}",devicePoint);
        boolean b = devicePointService.updateById(devicePoint);
        log.info("返回值：{}",b);
        return b ? Res.ok() : Res.fail();
    }

    /**
     * 分页查询
     *
     * @author : juzipi123
     * @param vo 筛选条件
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("/paginQuery")
    public Res<PageResult<DevicePointPaginResponseVO>> paginQuery(DevicePointPaginReqVO vo) {
        log.info("请求参数：{}",vo);
        PageResult<DevicePointPaginResponseVO> devicePointPaginResponseVOPageResult = devicePointService.paginQuery(vo);
        log.info("返回值：{}",devicePointPaginResponseVOPageResult);
        return Res.ok(devicePointPaginResponseVOPageResult);
    }


    /**
     * 通过主键删除数据
     *
     * @author : juzipi123
     * @param projectId 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @DeleteMapping("/removeById")
    public Res<String> removeById(String projectId) {
        log.info("请求参数:{}",projectId);
        boolean b = devicePointService.removeById(projectId);
        log.info("返回值：{}",b);
        return b ? Res.ok() : Res.fail();
    }

    /**
     * 根据 ‘企业上传数据 开始时间’ 查询 设备点位表
     *
     * @author : juzipi123
     * @param startTime 企业上传数据 开始时间
     * @return 查询结果
     */
    @ApiOperation("根据 ‘企业上传数据 开始时间’ 查询")
    @GetMapping("/selectByStartTime")
    public Res<List<DevicePoint>> selectByStartTime(String startTime) {
        log.info("请求参数:{}",startTime);
        List<DevicePoint> devicePoints = devicePointService.selectByStartTime(startTime);
        log.info("返回值：{}",devicePoints);
        return Res.ok(devicePoints);
    }

    @ApiOperation("信息分页查询")
    @GetMapping("/pageList")
    public Res<PageResult<DevicePoint>> pageList(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                                          @RequestParam(name = "pageSize", required = false, defaultValue = "15") int pageSize,
                                          @RequestParam(name = "condition", required = false) String conditionJson) {
        return Res.ok(devicePointService.pageList(conditionJson, pageNum, pageSize));
    }

    @ApiOperation("更新或新增")
    @PostMapping("/saveOrUpdate")
    public Res saveOrUpdate (@RequestBody DevicePoint devicePoint){
        return Res.ok(devicePointService.saveOrUpdate(devicePoint));
    }
}
